<template>
    <div>
        <Form :label-width="100" ref="formInline" :model="formInline">
            <Row :gutter="16" class="query-form2" >
                <i-col span="8">
                    <FormItem label="组织机构：">
                        <cascaderDom ref="existOrg" @setOrgInfo="setOrgName" :orgTree="true" :rootOrgId="rootOrgId" :currentOrgId = "dataToView.companyId" :currentOrgName = "dataToView.companyName"
                                     :setCurrentName="{value:dataToView.companyId, label:dataToView.companyName}"></cascaderDom>
                    </FormItem>
                </i-col>
                <i-col span="8">
                    <FormItem label="上报时间：">
                        <DatePicker v-model="formInline.reportDate" type="month" style="width: 100%" format="yyyy-MM" placeholder="请选择统计月份"></DatePicker>
                    </FormItem>
                </i-col>
                <i-col span="8">
                    <div style="float: right;">
                        <Button class="ivu-btn-query"  icon="md-search"  @click="query">查询</Button>
                        <Button class="ivu-btn-reset" style="margin-left: 10px" @click="cancel">重置</Button>
                    </div>
                </i-col>
            </Row>
        </Form>
        <Table ref='table' style="margin-top: 10px" :loading="loading" border :columns="columns"
               :data="infos" width="100%" :height="tableHeight" highlight-row stripe size="small"></Table>
    </div>
</template>
<script>

    import {Api} from '../../../api'
    var cascaderDom = () => import('@/components/cascader-dom.vue');
    import {sessionStorage} from '@/common/cacheUtils.js';

    export default {
        name: "apsgCensus",
        components:{
            cascaderDom
        },
        data(){
            return{
                loading: false,
                tableHeight: '',
                formInline: {},
                rootOrgId: '',
                columns: [
                    {
                        type: 'index',
                        title: '序号',
                        width: 80,
                        align: 'center',
                        fixed: 'left',
                    },
                    {
                        title: '单位名称',
                        align: 'center',
						fixed: 'left',
                        key: 'orgName',
                        width: 150,
                        render:(h,params)=>{
                            return h('div',[
                                h('span',params.row.orgName || '--')
                            ]);
                        }
                    },
                    {
                        title: '隐患数量',
                        align: 'center',
                        children: [
                            {
                                title: 'I',
                                align: 'center',
                                children: [
                                    {
                                        title: '上期未消除',
                                        align: 'center',
                                        key: 'lastCount1',
                                        minWidth: 100,
                                    },
                                    {
                                        title: '本期上报',
                                        align: 'center',
                                        key: 'totalCount1',
                                        minWidth: 90,
                                    },
                                    {
                                        title: '本期消除',
                                        align: 'center',
                                        key: 'handleCount1',
                                        minWidth: 90,
                                    },
                                    {
                                        title: '本期未消除',
                                        align: 'center',
                                        key: 'legacyCount1',
                                        minWidth: 100,
                                    }
                                ]
                            },
                            {
                                title: 'II',
                                align: 'center',
                                children: [
                                    {
                                        title: '上期未消除',
                                        align: 'center',
                                        key: 'lastCount2',
                                        minWidth: 100,
                                    },
                                    {
                                        title: '本期上报',
                                        align: 'center',
                                        key: 'totalCount2',
                                        minWidth: 90,
                                    },
                                    {
                                        title: '本期消除',
                                        align: 'center',
                                        key: 'handleCount2',
                                        minWidth: 90,
                                    },
                                    {
                                        title: '本期未消除',
                                        align: 'center',
                                        key: 'legacyCount2',
                                        minWidth: 100,
                                    }
                                ]
                            },
                            {
                                title: 'III',
                                align: 'center',
                                children: [
                                    {
                                        title: '上期未消除',
                                        align: 'center',
                                        key: 'lastCount3',
                                        minWidth: 100,
                                    },
                                    {
                                        title: '本期上报',
                                        align: 'center',
                                        key: 'totalCount3',
                                        minWidth: 90,
                                    },
                                    {
                                        title: '本期消除',
                                        align: 'center',
                                        key: 'handleCount3',
                                        minWidth: 90,
                                    },
                                    {
                                        title: '本期未消除',
                                        align: 'center',
                                        key: 'legacyCount3',
                                        minWidth: 100,
                                    }
                                ]
                            },
                            {
                                title: 'IV',
                                align: 'center',
                                children: [
                                    {
                                        title: '上期未消除',
                                        align: 'center',
                                        key: 'lastCount4',
                                        minWidth: 100,
                                    },
                                    {
                                        title: '本期上报',
                                        align: 'center',
                                        key: 'totalCount4',
                                        minWidth: 90,
                                    },
                                    {
                                        title: '本期消除',
                                        align: 'center',
                                        key: 'handleCount4',
                                        minWidth: 90,
                                    },
                                    {
                                        title: '本期未消除',
                                        align: 'center',
                                        key: 'legacyCount4',
                                        minWidth: 100,
                                    }
                                ]
                            },
                            {
                                title: '总数',
                                align: 'center',
                                children: [
                                    {
                                        title: '上期未消除',
                                        align: 'center',
                                        key: 'lastCount',
                                        minWidth: 100,
                                    },
                                    {
                                        title: '本期上报',
                                        align: 'center',
                                        key: 'totalCount',
                                        minWidth: 90,
                                    },
                                    {
                                        title: '本期消除',
                                        align: 'center',
                                        key: 'handleCount',
                                        minWidth: 90,
                                    },
                                    {
                                        title: '本期未消除',
                                        align: 'center',
                                        key: 'legacyCount',
                                        minWidth: 100,
                                    }
                                ]
                            },
                        ]
                    }
                ],
                infos: [],
                dataToView: {}
            }
        },
        mounted(){
            this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 103;
            let user = sessionStorage('userId').getObject('userId');
            this.rootOrgId = user.companyId;
            this.formInline.orgId = user.companyId;
            this.formInline.userId = user.userId;
            this.formInline.reportDate = this.formatDate(new Date(), 'yyyy-MM');
            this.dataToView = {companyName: user.companyName, companyId: user.companyId};
            setTimeout(() => {
                this.$refs.existOrg.setCurrentOrg();
            }, 100);
            this.query();
        },
        methods:{
            query(){
                this.loading = true;
                if(this.formInline.reportDate!=undefined&&this.formInline.reportDate!=''){
                    let d = new Date(this.formInline.reportDate + '-01');
                    this.formInline.month = this.formatDate(d, "yyyy-MM")
                    this.formInline.lastMonth =  d.getFullYear() + '-' + d.getMonth();
                }else{
                    this.formInline.month = this.formatDate(new Date(), "yyyy-MM")
                    this.formInline.lastMonth =  new Date().getFullYear() + '-' + new Date().getMonth();
                }
                this.$http.get(Api.apsgInverstigation+ '/count', {
                    params: this.formInline
                }).then(function(res){
                    this.infos = res.data;
                    this.loading = false;
                });
            },
            cancel(){
                this.formInline = {};
                let user = sessionStorage('userId').getObject('userId');
                this.formInline.orgId = user.companyId;
                this.formInline.userId = user.userId;
                this.infos = [];
            },
            setOrgName(data){
                this.formInline.orgId = data[data.length - 1];
                this.formInline.userId = "";
            },
            formatDate(date, fmt) {
                if (typeof date == 'string') {
                    return date;
                }
                if (!fmt) fmt = "yyyy-MM-dd hh:mm:ss";
                if (!date || date == null) return null;
                let o = {
                    'M+': date.getMonth() + 1, // 月份
                    'd+': date.getDate(), // 日
                    'h+': date.getHours(), // 小时
                    'm+': date.getMinutes(), // 分
                    's+': date.getSeconds(), // 秒
                    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
                    'S': date.getMilliseconds() // 毫秒
                }
                if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
                for (let k in o) {
                    if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
                }
                return fmt
            },
        }
    }
</script>

<style scoped>

</style>
